package org.elepower.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.elepower.dao.*;
import org.elepower.pojo.PsInspectiontaskDetails;
import org.elepower.pojo.PsInspectiontask_main;
import org.elepower.pojo.PsSolvetaskMain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/solveReturn")
public class SolveReturnController {
    @Autowired
    private SolveDetailDao solveDetailDao;
    @Autowired
    private SolverDao solverDao;
    @Autowired
    private SolveMainDao solveMainDao;
    @Autowired
    private InspectiontaskDetailsDao inspectiontaskDetailsDao;
    @Autowired
    private LineDao lineDao;

    @RequestMapping("/index")
    public String index() throws Exception{
        return "system/solveReturn/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, PsSolvetaskMain psSolvetaskMain, HttpServletRequest request) throws Exception {

        Map<String, Object> map = new HashMap<>();

        Page<PsSolvetaskMain> pager = PageHelper.startPage(page, limit);
        List<PsSolvetaskMain> list = solveMainDao.getNoCancelTask(psSolvetaskMain);
        request.setAttribute("list",list);
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", list);
        map.put("count", pager.getTotal());
        return map;
    }

    @RequestMapping("/huizhi")
    public String huizhi(Integer taskId, HttpSession session) throws Exception{
        session.setAttribute("solveTaskId",taskId);
        return "system/solveReturn/solveReturn";
    }


    /**
     * 开始执行
     */
    @RequestMapping("/open")
    @ResponseBody
    public Map<String,Object> opne(@RequestParam("taskId") Integer taskId) throws Exception{
        Map<String,Object> map = new HashMap<>();
        //开始执行,修改任务状态
        solveMainDao.updateSolveState(3,"执行中",taskId);
        map.put("result",true);
        return map;
    }



    /**
     * 修改
     */
    @RequestMapping("/save")
    @ResponseBody
    public Map<String,Object> save(@RequestParam("taskFinishDesc")String taskFinishDesc,@RequestParam("taskNotes")String taskNotes,@RequestParam("taskFinishReport")String taskFinishReport,HttpSession session) throws Exception{
        Map<String,Object> map = new HashMap<>();
        Integer id = (Integer) session.getAttribute("solveTaskId");
        //封装信息
        PsSolvetaskMain psSolvetaskMain = new PsSolvetaskMain();
        psSolvetaskMain.setTaskFinishDesc(taskFinishDesc);
        psSolvetaskMain.setTaskFinishReport(taskFinishReport);
        psSolvetaskMain.setTaskNotes(taskNotes);
        psSolvetaskMain.setId(id);
        //修改回执信息
        solveMainDao.returnSolveTask(psSolvetaskMain);
        map.put("result",true);
        return map;
    }
    /**
     * 回执
     */
    @RequestMapping("/upload")
    @ResponseBody
    public Map<String,Object> upload(@RequestParam("taskFinishDesc")String taskFinishDesc,@RequestParam("taskNotes")String taskNotes,@RequestParam("taskFinishReport")String taskFinishReport,HttpSession session) throws Exception{
        Map<String,Object> map = new HashMap<>();
        Integer id = (Integer) session.getAttribute("solveTaskId");
        //封装信息
        PsSolvetaskMain psSolvetaskMain = new PsSolvetaskMain();
        psSolvetaskMain.setTaskFinishDesc(taskFinishDesc);
        psSolvetaskMain.setTaskFinishReport(taskFinishReport);
        psSolvetaskMain.setTaskNotes(taskNotes);
        psSolvetaskMain.setId(id);
        //修改回执信息
        solveMainDao.returnSolveTask(psSolvetaskMain);
        //修改任务状态
        solveMainDao.updateSolveState(4,"已完成",psSolvetaskMain.getId());
        map.put("result",true);
        return map;
    }

    @RequestMapping("/shenchaIndex")
    public String shenchaIndex(Integer taskId,HttpSession session){
        session.setAttribute("solveTaskId",taskId);
        return "system/solveReturn/shencha";
    }

    @RequestMapping("/shencha")
    @ResponseBody
    public Map<String,Object> shencha(Integer state,HttpSession session) throws Exception{
        Map<String,Object> map = new HashMap<>();
        Integer id = (Integer) session.getAttribute("solveTaskId");
        System.out.println(state);
        //判断是否通过
        if(state == 0){
            //通过
            //修改任务状态
            solveMainDao.updateSolveState(5,"审查通过",id);
            //修改消缺员状态
            solverDao.updateTaskIdSolverState(id);
            //根据消缺字表获取缺陷编号,更改缺陷状态
            inspectiontaskDetailsDao.updateIsSolve(id);
            //修改线路状态
            //查询线路
            System.out.println(id);
            List<PsInspectiontaskDetails> list = inspectiontaskDetailsDao.getFuck(id);
            //循环修改线路状态
            for (PsInspectiontaskDetails ps: list) {
                System.out.println(ps.getLineCode());
                lineDao.updateStateForCode(ps.getLineCode());
            }
        }else if(state == 1){
            //未通过
            //修改任务状态
            solveMainDao.updateSolveState(6,"驳回",id);
        }
        map.put("result",true);
        return map;
    }
}
